package com.lagou.servlet;

import com.lagou.model.User;
import com.lagou.service.UserServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;


@WebServlet(name = "LoginServlet",urlPatterns = "/login")
public class LoginServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String username = request.getParameter("username");

        String password = request.getParameter("password");

        UserServiceImpl userService = new UserServiceImpl();
        User user = userService.userLoginSerivce(new User(username, password));
        if(user==null){
            request.setAttribute("msg", "登录不成功，请重新输入");
            // 请求转发
            request.getRequestDispatcher("index.jsp").forward(request,response);
        }else{
            // 将登录成功的用户信息放入session中,实现多个请求的共享
            request.getSession().setAttribute("user",user);
            // 重定向
            request.getRequestDispatcher("/showAllUserServlet").forward(request,response);

        }
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doPost(request,response);
    }
}
